package edu.pku.sei.act.classGenerator;

import edu.pku.sei.act.taskStruct.*;
import java.util.Collection;
import java.util.Iterator;
import java.util.Set;

 public class TransitionClassGenerator {  
  protected static String nl;
  public static synchronized TransitionClassGenerator create(String lineSeparator)
  {
    nl = lineSeparator;
    TransitionClassGenerator result = new TransitionClassGenerator();
    nl = null;
    return result;
  }

  public final String NL = nl == null ? (System.getProperties().getProperty("line.separator")) : nl;
  protected final String TEXT_1 = NL + "package edu.pku.sei.act.";
  protected final String TEXT_2 = ".";
  protected final String TEXT_3 = ";" + NL + "" + NL + "import edu.pku.sei.act.actools.util.EnvInfo;" + NL + "import edu.pku.sei.act.actools.util.TaskMessage;" + NL + "import edu.pku.sei.act.";
  protected final String TEXT_4 = ".";
  protected final String TEXT_5 = ";" + NL + "import android.content.BroadcastReceiver;" + NL + "import android.content.Context;" + NL + "import android.content.Intent;" + NL + "import android.os.RemoteException;" + NL + "" + NL + "public class ";
  protected final String TEXT_6 = " extends BroadcastReceiver {" + NL + "" + NL + "\t@Override" + NL + "\tpublic void onReceive(Context context, Intent intent) {" + NL + "\t";
  protected final String TEXT_7 = "\t" + NL + "\t \t";
  protected final String TEXT_8 = NL + "\t try {\t" + NL + "\t \t\t";
  protected final String TEXT_9 = NL + "\t \t\t" + NL + "\t\t\t\tTaskMessage msg";
  protected final String TEXT_10 = " = ";
  protected final String TEXT_11 = ".communication.";
  protected final String TEXT_12 = ";" + NL + "\t\t\t\tmsg";
  protected final String TEXT_13 = ".setSender(";
  protected final String TEXT_14 = ".getCm().toString());" + NL + "\t\t\t\t";
  protected final String TEXT_15 = ".getEe().notifyRWrite(EnvInfo.HEAD_TUPLE + \",\" + msg";
  protected final String TEXT_16 = ", 0, -1);" + NL + "\t\t\t\t\t\t" + NL + "\t\t\t";
  protected final String TEXT_17 = NL + "\t\t\t  " + NL + "\t\t\t\t";
  protected final String TEXT_18 = ".setState(";
  protected final String TEXT_19 = ".state.";
  protected final String TEXT_20 = ");" + NL + "\t\t} catch (RemoteException e) {" + NL + "\t\t\te.printStackTrace();" + NL + "\t\t} " + NL + "\t\t " + NL + "\t\t";
  protected final String TEXT_21 = "\t\t\t\t" + NL + "\t\t\t\tString rs = (String) intent.getExtras().get(\"arg1\");" + NL + "\t\t\t\tTaskMessage msg = new TaskMessage(rs);" + NL + "\t\t\t";
  protected final String TEXT_22 = "\t\t\t\t" + NL + "\t\t\t\t\tif (msg.getContent().equals(\"";
  protected final String TEXT_23 = "\")) {" + NL + "\t\t\t\t\t\ttry {" + NL + "\t\t\t\t\t\t\t";
  protected final String TEXT_24 = NL + "\t\t\t\t\t\t\t\tTaskMessage msg = ";
  protected final String TEXT_25 = ".communication.";
  protected final String TEXT_26 = ";" + NL + "\t\t\t\t\t\t\t\tmsg.setSender(";
  protected final String TEXT_27 = ".getCm().toString());" + NL + "\t\t\t\t\t\t\t\t";
  protected final String TEXT_28 = ".getEe().notifyRWrite(EnvInfo.HEAD_TUPLE + \",\" + msg, 0, -1);" + NL + "\t\t\t\t\t\t\t";
  protected final String TEXT_29 = "\t\t\t\t\t\t\t\t\t\t\t\t" + NL + "\t\t\t\t\t\t\t";
  protected final String TEXT_30 = ".setState(";
  protected final String TEXT_31 = ".state.";
  protected final String TEXT_32 = ");" + NL + "\t\t\t\t\t\t} catch (RemoteException e1) {" + NL + "\t\t\t\t\t\t\te1.printStackTrace();" + NL + "\t\t\t\t\t\t}" + NL + "\t\t\t\t\t}\t\t\t \t" + NL + "\t";
  protected final String TEXT_33 = NL + "\t try {" + NL + "\t\t\tThread.sleep(";
  protected final String TEXT_34 = ");" + NL + "\t\t} catch (InterruptedException e) {" + NL + "\t\t\te.printStackTrace();" + NL + "\t\t}" + NL + "\t\t";
  protected final String TEXT_35 = NL + "\t\tif(";
  protected final String TEXT_36 = ".getCurrentState().equals(";
  protected final String TEXT_37 = ".state.";
  protected final String TEXT_38 = ")) {" + NL + "\t\t\ttry {" + NL + "\t\t\t\t";
  protected final String TEXT_39 = ".setState(";
  protected final String TEXT_40 = ".state.";
  protected final String TEXT_41 = ");" + NL + "\t\t\t\t";
  protected final String TEXT_42 = NL + "\t\t\t\t\tTaskMessage msg = ";
  protected final String TEXT_43 = ".communication.";
  protected final String TEXT_44 = ";" + NL + "\t\t\t\t\tmsg.setSender(";
  protected final String TEXT_45 = ".getCm().toString());" + NL + "\t\t\t\t\t";
  protected final String TEXT_46 = ".getEe().notifyRWrite(EnvInfo.HEAD_TUPLE + \",\" + msg, 0, -1);" + NL + "\t\t\t\t";
  protected final String TEXT_47 = NL + "\t\t\t} catch (RemoteException e) {" + NL + "\t\t\t\te.printStackTrace();" + NL + "\t\t\t}" + NL + "\t\t}\t \t" + NL + "\t";
  protected final String TEXT_48 = NL + NL + "}" + NL + "" + NL + " ";
  protected final String TEXT_49 = NL;

 public String generate(String taskName, Role role, Transition transition)
  {
    final StringBuffer stringBuffer = new StringBuffer();
    stringBuffer.append(TEXT_1);
    stringBuffer.append( NameGenerator.genTaskPackageName(taskName));
    stringBuffer.append(TEXT_2);
    stringBuffer.append( NameGenerator.genRolePackageName(role.getName()));
    stringBuffer.append(TEXT_3);
    stringBuffer.append( NameGenerator.genTaskPackageName(taskName));
    stringBuffer.append(TEXT_4);
    stringBuffer.append( NameGenerator.genTaskProcessorName(taskName));
    stringBuffer.append(TEXT_5);
    stringBuffer.append( NameGenerator.genTransitionName(transition.getName()));
    stringBuffer.append(TEXT_6);
     if(!NameGenerator.genTransitionTypeName("TrigByDelay").equals(transition.getType())){
	 
    stringBuffer.append(TEXT_7);
    if(transition.getPostStateIds().size() == 1) {
	 		String postStateId = transition.getPostStateIds().iterator().next();	 		
	 		String postStateName = NameGenerator.genStateName(role.getStateById(postStateId).getName());	 		 
	 	 
    stringBuffer.append(TEXT_8);
    
	 		Set<String> msgIds = transition.getPostTaskMessageIdsById(postStateId);
	 		int cnt = 0;
	 		for(String msgId : msgIds)
	 		{
	 			TaskMessage msg = role.getTaskMessageById(msgId);	 			
	 		
    stringBuffer.append(TEXT_9);
    stringBuffer.append( cnt);
    stringBuffer.append(TEXT_10);
    stringBuffer.append( NameGenerator.genRoleName(role.getName()));
    stringBuffer.append(TEXT_11);
    stringBuffer.append( NameGenerator.genTaskMessageName(msg.getName()));
    stringBuffer.append(TEXT_12);
    stringBuffer.append( cnt);
    stringBuffer.append(TEXT_13);
    stringBuffer.append( NameGenerator.genTaskProcessorName(role.getName()));
    stringBuffer.append(TEXT_14);
    stringBuffer.append( NameGenerator.genTaskProcessorName(role.getName()));
    stringBuffer.append(TEXT_15);
    stringBuffer.append( cnt);
    stringBuffer.append(TEXT_16);
    
				++cnt;
			} 
    stringBuffer.append(TEXT_17);
    stringBuffer.append( NameGenerator.genTaskProcessorName(taskName));
    stringBuffer.append(TEXT_18);
    stringBuffer.append( NameGenerator.genRoleName(role.getName()));
    stringBuffer.append(TEXT_19);
    stringBuffer.append( postStateName);
    stringBuffer.append(TEXT_20);
     }else{ 
    stringBuffer.append(TEXT_21);
      Set<String> ids = transition.getPostStateIds();
				for(Iterator<String> it = ids.iterator(); it.hasNext(); )
				{					
					String id = it.next();
					String condition = transition.getPostStateConditionById(id);
					State state = role.getStateById(id);
		 	
    stringBuffer.append(TEXT_22);
    stringBuffer.append( condition);
    stringBuffer.append(TEXT_23);
     
							Set<String> msgIds = transition.getPostTaskMessageIdsById(state.getId());
							for(String msgId : msgIds)
							  {
								TaskMessage msg = role.getTaskMessageById(msgId);
							
    stringBuffer.append(TEXT_24);
    stringBuffer.append( NameGenerator.genRoleName(role.getName()));
    stringBuffer.append(TEXT_25);
    stringBuffer.append( NameGenerator.genTaskMessageName(msg.getName()));
    stringBuffer.append(TEXT_26);
    stringBuffer.append( NameGenerator.genTaskProcessorName(taskName));
    stringBuffer.append(TEXT_27);
    stringBuffer.append( NameGenerator.genTaskProcessorName(taskName));
    stringBuffer.append(TEXT_28);
     } 
							
    stringBuffer.append(TEXT_29);
    stringBuffer.append(NameGenerator.genTaskProcessorName(taskName));
    stringBuffer.append(TEXT_30);
    stringBuffer.append( NameGenerator.genRoleName(role.getName()));
    stringBuffer.append(TEXT_31);
    stringBuffer.append( NameGenerator.genStateName(state.getName()));
    stringBuffer.append(TEXT_32);
     
				}
		} 
	}else{
	 
    stringBuffer.append(TEXT_33);
    stringBuffer.append( transition.getDelayTime());
    stringBuffer.append(TEXT_34);
     Set<String> preIds = transition.getPreStateIds();			
		   State preState = role.getStateById(preIds.iterator().next());
		   Set<String> postIds = transition.getPostStateIds();			
		   State postState = role.getStateById(postIds.iterator().next());		   
		
    stringBuffer.append(TEXT_35);
    stringBuffer.append( NameGenerator.genTaskProcessorName(taskName));
    stringBuffer.append(TEXT_36);
    stringBuffer.append( NameGenerator.genRoleName(role.getName()));
    stringBuffer.append(TEXT_37);
    stringBuffer.append( NameGenerator.genStateName(preState.getName()));
    stringBuffer.append(TEXT_38);
    stringBuffer.append( NameGenerator.genTaskProcessorName(taskName));
    stringBuffer.append(TEXT_39);
    stringBuffer.append( NameGenerator.genRoleName(role.getName()));
    stringBuffer.append(TEXT_40);
    stringBuffer.append( NameGenerator.genStateName(postState.getName()));
    stringBuffer.append(TEXT_41);
     Set<String> msgIds = transition.getPostTaskMessageIdsById(postState.getId());
				for(String msgId : msgIds)
				  {
					TaskMessage msg = role.getTaskMessageById(msgId);
				
    stringBuffer.append(TEXT_42);
    stringBuffer.append( NameGenerator.genRoleName(role.getName()));
    stringBuffer.append(TEXT_43);
    stringBuffer.append( NameGenerator.genTaskMessageName(msg.getName()));
    stringBuffer.append(TEXT_44);
    stringBuffer.append( NameGenerator.genTaskProcessorName(taskName));
    stringBuffer.append(TEXT_45);
    stringBuffer.append( NameGenerator.genTaskProcessorName(taskName));
    stringBuffer.append(TEXT_46);
     } 
				 
    stringBuffer.append(TEXT_47);
    
	}
    stringBuffer.append(TEXT_48);
    stringBuffer.append(TEXT_49);
    return stringBuffer.toString();
  }
}